package com.soso.tg.xxx.server;

import java.io.File;
import java.io.Serializable;
import java.net.MalformedURLException;
import java.util.Date;

import com.soso.tg.xxx.client.GreetingService;
import com.soso.tg.xxx.service.UserService;
import com.soso.tg.xxx.shared.FieldVerifier;
import com.soso.tg.xxx.vo.User;
import com.caucho.hessian.client.HessianProxyFactory;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

/**
 * The server side implementation of the RPC service.
 */
@SuppressWarnings("serial")
public class GreetingServiceImpl extends RemoteServiceServlet implements
		GreetingService {

	public String greetServer(String input) throws IllegalArgumentException {
		// Verify that the input is valid. 
		if (!FieldVerifier.isValidName(input)) {
			// If the input is not valid, throw an IllegalArgumentException back to
			// the client.
			throw new IllegalArgumentException(
					"Name must be at least 4 characters long");
		}

		String serverInfo = getServletContext().getServerInfo();
		String userAgent = getThreadLocalRequest().getHeader("User-Agent");
		int i=(int)(Math.random()*1000);
		try {
			Thread.sleep(i);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(i);
		// Escape data from the client to avoid cross-site script vulnerabilities.
		input = escapeHtml(input);
		userAgent = escapeHtml(userAgent);

		testHessein(input);
		
		
		return  input;
	}

	private void testHessein(String name) {
		
		String url = "http://localhost:8080/diaoyudao/service";
		  HessianProxyFactory factory = new HessianProxyFactory();
		  UserService userService;
		try {
			userService = (UserService)factory.create(UserService.class, url);
			User user = userService.getUser(name);
			System.out.println("My name is " + user.getName() + "! I am come from " + user.getCountry());
			
			File file=new File("E:/submit_win.py");
			File temp=userService.getUserFile(file);
		
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 		  
	}
	/**
	 * Escape an html string. Escaping data received from the client helps to
	 * prevent cross-site script vulnerabilities.
	 * 
	 * @param html the html string to escape
	 * @return the escaped string
	 */
	private String escapeHtml(String html) {
		if (html == null) {
			return null;
		}
		return html.replaceAll("&", "&amp;").replaceAll("<", "&lt;")
				.replaceAll(">", "&gt;");
	}
	
}
